import { TextInput, DateTimeInput, RefreshButton } from 'react-admin';
import { FileInput, FileField, Button, SimpleForm ,SelectInput,required} from 'react-admin';
import { fileUploadProvider } from '../../dataProvider/DataProvider'
import { dataProvider } from '../../dataProvider/DataProvider'

import { useRefresh,useNotify} from 'react-admin';
import React, { useState } from 'react'

export const UploadContactLogBtn = (props) => {
    const [show, setShow] = useState(false);
    const [contactEvents,setContactEvents] = useState([]);
    const { uploadCode,label } = props;

    const onSubmit = (data,refresh,notify) => {
      
        if (data == null || data.attachment == null) {
            return;
        }
        const param = {
            "id":data.id,
            "uploadCode":"UPLOAD_CODE_CONTACT_LOG"
        };
        fileUploadProvider.uploadFile(data.attachment, param, (isSucceed)=>callback(isSucceed,refresh,notify))
    }

    const callback = (isSucceed,refresh,notify) => {
        
        if (isSucceed) {
            setShow(false);
            refresh();
            notify("上传成功！",{type:'info'})
        }
    }

    const onClick =() =>{
        
        if(!show){
            dataProvider.getContactEventDropdown(true,setContactEvents);
        }
        setShow(!show);
    }
    const refresh = useRefresh();
    const notify = useNotify();
    return (
       
        <div>
            <Button label={label} onClick={onClick} />
            {show ? (<SimpleForm onSubmit={(data)=>onSubmit(data,refresh,notify)}>
                <SelectInput label="通联活动名称" source="id" choices={contactEvents} 
                    onChange={r=>console.log(r)}
                    validate={required()}
                
                ></SelectInput>
                
                <FileInput label="上传通联记录（Excel文件）" source="attachment">
                    <FileField source="src" title="title" />
                </FileInput>
            </SimpleForm>) : null}
        </div>
    )
}